package com.ys.controller.api.finance;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.config.MenuId;
import com.ys.entity.finance.TcwWanliExchangeRate;
import com.ys.entity.sys.YsCol;
import com.ys.service.finance.TcwWanliExchangeRateService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

/**
 * 万里汇CNY计算汇率表
 */
@Controller
@RequestMapping("/api/inter/tcwWanliExchangeRate")
public class ApiInterTcwWanliExchangeRateController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterTcwWanliExchangeRateController.class);

    @Autowired
    private CommonApiService<TcwWanliExchangeRate> commonApiService;

    @Autowired
    private TcwWanliExchangeRateService tcwWanliExchangeRateService;


    /**
     * 设置查询的cond的sql设置
     * @param cond
     * @return
     */
    public TcwWanliExchangeRate setSeachSql(TcwWanliExchangeRate cond){

        cond.addLeftJoin("TCW_DepositPlatform D ON D.Code = A.DepositPlatformCode ")//万里汇账号资金
                .addField("D.PlatformName AS platformName")//收款平台名称
                .addField("D.Code AS depositPlatformCode")//账号编码
                .addField("D.Name AS name")//账号名称
                .addField("D.Mid AS depositPlatformMid")

        .addLeftJoin("TB_CompanyInfo O ON O.Vid = D.CompanyVid ")//公司信息表
                .addField("O.Vname AS vname")//公司名称

                //.setStatus(1)

        //排序
        .setOrderby("A.RefreshTime DESC,D.Code DESC");

        cond = commonApiService.dataAuth(MenuId.TCW_WanliExchangeRate, cond);
        return  cond;
    }


    /**
     * 查询
     */
    @RequiresPermissions("tcwWanliExchangeRate:view")
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA search(@ModelAttribute TcwWanliExchangeRate cond){
        cond = setSeachSql(cond);
        return commonApiService.searchLay(cond);
    }


    /**
     * 弹出框查询
     */
    @RequestMapping("/searchForModal")
    @ResponseBody
    public LayuiTableVoJA searchForModal(@ModelAttribute TcwWanliExchangeRate cond){
        
    	cond.setStatus(1);
        
        cond.addLeftJoin("TCW_DepositPlatform B ON B.Code = A.DepositPlatformCode")
        	.addField("B.Name depositPlatformName")
        	.addField("B.PlatformName platformName")
        	
        	.addLeftJoin("TB_CompanyInfo C ON C.Vid = B.CompanyVid")
        	.addField("C.Vid companyVid")
        	.addField("C.Vname companyVname");
        
        return commonApiService.searchLay(cond);
    }

    /**
     * 导出
     */
    @RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TcwWanliExchangeRate cond,String conditions){
        try {
            cond.setPage(0).setSize(999999);
            cond = setSeachSql(cond);
            //获取菜单字段数据
            List<YsCol> listField = new YsCol()
                    .setMenuId(MenuId.TCW_WanliExchangeRate)
                    .setFrontShow(1)
                    .setOrderby("A.SortNo asc")
                    .queryList();
            //获取数据
            List<Map<String, Object>> listData = cond.queryMap();

            //处理数据
            ExportExcelUtils<TcwWanliExchangeRate> util = new ExportExcelUtils<>(new TcwWanliExchangeRate());
            String toFilePath = util.exportExcel(listData, listField, "万里汇CNY计算汇率表");
            UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
            return R.ok(dto.getUrl());
        }catch (Exception e){
            logger.error("导出出错：",e.getMessage(),e);
            return R.error("导出失败，请稍后重试！");
        }
    }



    /**
     * 新增
     */
    @RequestMapping("/toAdd")
    @ResponseBody
    public R toSave(HttpServletRequest request, @RequestParam Map<String, Object> params){
        try {
            //新增
            return tcwWanliExchangeRateService.add(params,MenuId.TCW_WanliExchangeRate);
        } catch (Exception e) {
            logger.error("新增出错：",e.getMessage(),e);
            return R.error(e.getCause().getMessage());
        }
    }


    /**
     * 修改
     */
    @RequestMapping("/toEdit")
    @ResponseBody
    public R toEdit(HttpServletRequest request, @RequestParam Map<String, Object> params){
        try {
            //修改
            return tcwWanliExchangeRateService.update(params,MenuId.TCW_WanliExchangeRate);
        } catch (Exception e) {
            logger.error("修改出错：",e.getMessage(),e);
            return R.error(e.getCause().getMessage());
        }
    }



    /**
     * 导入当日汇率
     */
    @RequestMapping("/updateExchangeRate")
    @ResponseBody
    public R updateExchangeRate(HttpServletRequest request, @RequestParam Map<String, Object> params){
        try {
            if(params.get("countTimeStart")==null){
                return R.error("未选择开始日期");
            }
            if(params.get("countTimeEnd")==null){
                return R.error("未选择结束日期");
            }
            String countTimeStart = params.get("countTimeStart").toString();
            String countTimeEnd = params.get("countTimeEnd").toString();
            return tcwWanliExchangeRateService.updateExchangeRate(countTimeStart,countTimeEnd);
        } catch (Exception e) {
            logger.error("导入当日汇率出错：",e.getMessage(),e);
            return R.error(e.getCause().getMessage());
        }
    }



    /**
     * 弹出框
     */
    @RequestMapping("/searchForModalByGbp")
    @ResponseBody
    public LayuiTableVoJA searchForModalByGbp(@ModelAttribute TcwWanliExchangeRate cond){
        cond = setSeachSql(cond);
        cond.setCurrencyCode("GBP");
        return commonApiService.searchLay(cond);
    }


    /**
     * 弹出框
     */
    @RequestMapping("/searchForModalByUsd")
    @ResponseBody
    public LayuiTableVoJA searchForModalByUsd(@ModelAttribute TcwWanliExchangeRate cond){
        cond = setSeachSql(cond);
        cond.setCurrencyCode("USD");
        return commonApiService.searchLay(cond);
    }


    /**
     * 弹出框
     */
    @RequestMapping("/searchForModalByEur")
    @ResponseBody
    public LayuiTableVoJA searchForModalByEur(@ModelAttribute TcwWanliExchangeRate cond){
        cond = setSeachSql(cond);
        cond.setCurrencyCode("EUR");
        return commonApiService.searchLay(cond);
    }




}